package com.zx.sercurity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;

import androidx.core.content.ContextCompat;

import java.io.File;

/**
 * MT管理器风控
 */
public class MTRisk implements RiskControlInterface<RiskResult.MTManagerRiskResult> {

    private static final String TAG = "MTRisk";

    @Override
    public RiskResult.MTManagerRiskResult riskCheck(Context context) {
        if (isInstalledMT(context) || isMTFolderExists(context)) return new RiskResult.MTManagerRiskResult(true) ;
        return new RiskResult.MTManagerRiskResult(false);
    }

    /**
     * 判断是否安装了mt管理器
     *
     * @param context context
     * @return
     */
    private boolean isInstalledMT(Context context) {
        PackageManager packageManager = context.getPackageManager();
        try {
            packageManager.getPackageInfo("bin.mt.plus", PackageManager.GET_ACTIVITIES);
            Log.i(TAG, "检查到安装了MT管理器");
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            Log.i(TAG, "未安装MT管理器");
            return false;
        }
    }

    /**
     * 判断内存下是否含有 MT2 文件夹
     * 此段代码未校验
     *
     * @param context context
     * @return
     */
    private boolean isMTFolderExists(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            // 小于 Android 6.0 (API 23)
            File file = new File("/MT2");
            Log.i(TAG, "安卓版本小于6.0");
            return file.exists();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            // Android 6.0 (API 23) 及以上，但小于 Android 10 (API 29)
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "安卓版本大于6.0，未授权存储权限");
                return false;
            } else {
                File file = new File("/MT2");
                Log.i(TAG, "安卓版本大于6.0");
                return file.exists();
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Android 10 (API 29) 及以上
            Log.i(TAG, "安卓版本大于10");
            //TODO： 无效代码
            return isFolderExistsInSharedStorage(context, "MT2");
        }
        return false;
    }

    /**
     * 判断共享存储中是否存在指定文件夹
     *
     * @param context    上下文
     * @param folderName 文件夹名称
     * @return true：存在；false：不存在
     */
    public boolean isFolderExistsInSharedStorage(Context context, String folderName) {
        ContentResolver resolver = context.getContentResolver();
        Uri uri = MediaStore.Files.getContentUri("external"); // 外部存储的 URI

        // 查询条件：文件夹名称
        String selection = MediaStore.Files.FileColumns.DISPLAY_NAME + "=?";
        String[] selectionArgs = new String[]{folderName};

        // 查询文件夹
        Cursor cursor = resolver.query(uri, null, selection, selectionArgs, null);
        Log.i("MTRisk", uri.toString());
        if (cursor != null) {
            boolean exists = cursor.getCount() > 0;
            Log.i("MTRisk", cursor.getCount() + "");
            cursor.close();
            return exists;
        }
        return false;
    }

}
